package com.loong.musicdemo.ui.mine.recent.fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Album;
import com.loong.musicdemo.ui.mine.adapter.MineAlbumAdapter;
import com.loong.musicdemo.util.dataDeal.AlbumDeal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecentAlbumFragment extends Fragment {
    private ProgressBar progressBar;
    private RecyclerView recent_albums_view;
    private SmartRefreshLayout refresh_album_list;
    private List<Album> albumList = new ArrayList<>();
    private List<Album> albumtemp = new ArrayList<>();
    private MineAlbumAdapter albumAdapter;

    public RecentAlbumFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recent_album, container, false);
        progressBar = view.findViewById(R.id.progress_bar);
        recent_albums_view = view.findViewById(R.id.recent_albums_view);
        refresh_album_list = view.findViewById(R.id.refresh_album_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recent_albums_view.setLayoutManager(layoutManager);
        new Thread(() -> {
            albumList = AlbumDeal.getNewestAlbumInfo();
            if (albumList != null) {
                List<Album> as = new ArrayList<>();
                if (albumList.size() > 10) {
                    as = albumList.subList(0, 10);
                    albumList = albumList.subList(10, albumList.size());
                    albumtemp.addAll(as);
                }else {
                    albumtemp = albumList;
                }
                albumAdapter = new MineAlbumAdapter(albumtemp,getContext());
                getActivity().runOnUiThread(() -> {
                    progressBar.setVisibility(View.GONE);
                    recent_albums_view.setAdapter(albumAdapter);
                });
            }
        }).start();
        refresh_album_list.setEnableRefresh(false);
        refresh_album_list.autoLoadmore();
        refresh_album_list.setOnLoadmoreListener(refreshlayout -> {
            new AlbumTask().execute();//加载后续数据
        });
        return view;
    }
    class AlbumTask extends AsyncTask<Album,Void, List<Album>> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<Album> albums) {
            super.onPostExecute(albums);
            progressBar.setVisibility(View.GONE);
            albumtemp.addAll(albums);
            albumAdapter.notifyDataSetChanged();
            refresh_album_list.finishLoadmore();
        }

        @Override
        protected List<Album> doInBackground(Album... albums) {
            if (albumList.size() <= 10) {
                refresh_album_list.setEnableLoadmore(false);//当数据不足10条,禁用加载
                return albumList;
            } else {
                List<Album> list = albumList.subList(0, 10);
                albumList = albumList.subList(10, albumList.size());
                return list;
            }
        }
    }
}
